package com.zmr.wx.mp.robot.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Component;

import com.zmr.wx.mp.robot.RebotProxy;
import com.zmr.wx.mp.utils.MessageUtil;

/**
 * 调用图灵机器人api接口，获取智能回复内容
 */
@Component("tlrebot")
public class TLRebotProxy implements RebotProxy
{
    @Value("${rebot.tl.apikey}")
    private String apikey;

    @Override
    public String botMessageReply(String content)
    {
        String result = "";
        try
        {
            String INFO = URLEncoder.encode(content, "utf-8");
            String getURL = "http://www.tuling123.com/openapi/api?key=" + apikey + "&info=" + INFO;
            String re = MessageUtil.getBotReply(getURL);
            JSONObject json = new JSONObject(re);
            result = json.getString("text");
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return result;
    }
}